package com.cloudfun.campusshare.controller;

import com.cloudfun.campusshare.common.constant.Code;
import com.cloudfun.campusshare.common.model.dto.RoleCreateDTO;
import com.cloudfun.campusshare.common.model.dto.RoleModifyDTO;
import com.cloudfun.campusshare.common.model.vo.RoleDetailVO;
import com.cloudfun.campusshare.common.model.vo.RoleListVO;
import com.cloudfun.campusshare.common.response.BaseResult;
import com.cloudfun.campusshare.common.response.BaseResultFactory;
import com.cloudfun.campusshare.common.response.PageResult;
import com.cloudfun.campusshare.common.security.AuthRequired;
import com.cloudfun.campusshare.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by Huqin on 2020/5/7
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/v1/role")
@Api(value = "角色模块", description = "角色模块")
@AuthRequired
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping
    public BaseResult<?> create(@ApiParam(value = "createDTO", required = true) @Validated({RoleCreateDTO.Create.class}) @RequestBody RoleCreateDTO createDTO) {
        roleService.create(createDTO);
        return BaseResultFactory.produceEmptyResult(Code.SUCCESS);
    }

    @PutMapping
    @ApiOperation(value = "编辑", notes = "编辑")
    BaseResult<?> modify(@ApiParam(value = "modifyDTO", required = true) @Valid @RequestBody RoleModifyDTO modifyDTO) {
        roleService.modify(modifyDTO);
        return BaseResultFactory.produceEmptyResult(Code.SUCCESS);
    }

    @GetMapping("/list")
    @ApiOperation(value = "列表")
    public BaseResult<PageResult<RoleListVO>> querySchoolList(Pageable pageable, @ApiParam(value = "关键字") @RequestParam(required = false) String keyword) {
        return BaseResultFactory.produceResult(Code.SUCCESS, roleService.search(pageable, keyword));
    }

    @ApiOperation(value = "删除", notes = "删除")
    @PostMapping("/delete")
    public BaseResult<?> projectDelete(@ApiParam(value = "id", required = true) @RequestParam String id) {
        roleService.delete(id);
        return BaseResultFactory.produceEmptyResult(Code.SUCCESS);
    }

    @ApiOperation(value = "详情", notes = "详情")
    @GetMapping("/detail")
    public BaseResult<RoleDetailVO> detail(@ApiParam(value = "id", required = true) @RequestParam String id) {
        RoleDetailVO roleDetailVO = roleService.detail(id);
        return BaseResultFactory.produceResult(Code.SUCCESS, roleDetailVO);
    }

    @ApiOperation(value = "启用\\停用", notes = "启用\\停用")
    @GetMapping("/enable")
    public BaseResult<?> enableOrDisable(@ApiParam(value = "id", required = true) @RequestParam String id,
                                         @ApiParam(value = "enable", required = true) @RequestParam Boolean enable) {
        roleService.enableOrDisable(id, enable);
        return BaseResultFactory.produceEmptyResult(Code.SUCCESS);
    }

    @ApiOperation(value = "角色简单列表", notes = "角色简单列表")
    @GetMapping("/simpleList")
    public BaseResult<List<RoleListVO>> simpleList() {
        List<RoleListVO> roleList = roleService.getSimpleList();
        return BaseResultFactory.produceResult(Code.SUCCESS, roleList);
    }

}
